<?php

namespace app\admin\controller;

use think\Request;

class Store extends Base
{

    public function getList(){
        $params = $this->request->param();
        $where = [];
        if(isset($params['begin_time']) && $params['begin_time']){
            $where['a.createDate'] = ['egt', $params['begin_time']];
        }
        if(isset($params['end_time']) && $params['end_time']){
            $where['a.createDate'] = ['lt', $params['end_time'] . ' 23:59:59'];
        }
        if(isset($params['begin_time']) && $params['begin_time'] && isset($params['end_time']) && $params['end_time']){
            $where['a.createDate'] = [['egt', $params['begin_time']], ['lt', $params['end_time'] . ' 23:59:59'], 'and'];
        }
        if(isset($params['name']) && $params['name']){
            $where['a.name'] = ['like', "%{$params['name']}%"];
        }

        $model = model('Store');
        $join = [
            ['__TYPE__ t','a.typeId=t.id'],
            ['__BUSINESSAREA__ b','b.id=a.businessAreaId']
        ];
        $field = "a.*,b.globalName,t.typeName";
        $list = $model->alias('a')->where($where)->join($join)->order('a.createDate desc')->field($field)->paginate(10);
        $total = $model->alias('a')->where($where)->join($join)->count();

        return $this->fetch('',[
            'datas' => $list,
            'total' => $total,
            'formget' => $_GET
        ]);
    }

    /**
     * 审核/关停/开启店铺
     */
    public function changestatus(Request $request){
        if($request->isPost()){
            $param = $request->param();
            if(empty($param['storeId'])){
                return show(config('status.error_code'),'参数错误','参数错误');
            }
            $Store = \app\admin\model\Store::get($param['storeId']);
            if($Store){
                if($Store->status == 0){
                    $Store->status = 1;
                    if($Store->save()){
                        return show(config('status.success_code'),'ok',$param['storeId']);
                    }else{
                        return show(config('status.error_code'),'审核失败','审核失败');
                    }
                }elseif ($Store->status == 1){
                    $Store->status = 2;
                    if($Store->save()){
                        return show(config('status.success_code'),'ok',$param['storeId']);
                    }else{
                        return show(config('status.error_code'),'关停失败','关停失败');
                    }
                }else{
                    $Store->status = 1;
                    if($Store->save()){
                        return show(config('status.success_code'),'ok',$param['storeId']);
                    }else{
                        return show(config('status.error_code'),'开启失败','开启失败');
                    }
                }
            }else{
                return show(config('status.error_code'),'店铺信息异常','店铺信息异常');
            }
        }
    }
}